/**
 * 2024/01/01 - 2024/12/31
 **/
package com.snail.camunda.engin.identity.manager;

import com.snail.camunda.engin.identity.dto.UserInsertDTO;
import com.snail.camunda.engin.identity.dto.UserModifyDTO;
import com.snail.camunda.engin.identity.dto.UserQueryDTO;
import com.snail.camunda.engin.identity.holder.GroupHolder;
import com.snail.camunda.engin.identity.holder.TenantHolder;
import com.snail.camunda.engin.identity.holder.UserHolder;
import com.snail.camunda.engin.identity.mapping.UserMapping;
import com.snail.camunda.engin.identity.vo.UserVO;
import com.snail.starter.commons.page.PageQO;
import com.snail.starter.commons.page.PageVO;
import org.camunda.bpm.engine.identity.Group;
import org.camunda.bpm.engine.identity.Tenant;
import org.camunda.bpm.engine.identity.User;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: (zg)
 * @Date: 2024/12/16 13:13
 * @Version: 1.0.0
 */
@Component
public class UserManager {

    @Resource
    UserHolder identityHolder;

    @Resource
    UserMapping identityMapping;

    @Resource
    TenantHolder tenantHolder;

    @Resource
    GroupHolder groupHolder;

    public String add(String tenantId, String groupId, UserInsertDTO dto) {
        Tenant tenant = tenantHolder.queryById(tenantId);
        Group group = groupHolder.queryById(groupId);
        return identityHolder.add(tenant, group, dto);
    }

    public boolean modify(String id, UserModifyDTO dto) {
        return identityHolder.modify(id, dto);
    }

    public UserVO queryById(String id) {
        User user = identityHolder.queryById(id);
        return identityMapping.entity2Vo(user);
    }

    public boolean deleteById(String id) {
        return identityHolder.deleteById(id);
    }

    public List<UserVO> list(UserQueryDTO dto) {
        List<User> users = identityHolder.list(dto);
        if (null == users || users.isEmpty()) {
            return new ArrayList<>();
        }
        return users.stream().map(identityMapping::entity2Vo).collect(Collectors.toList());
    }

    public PageVO<UserVO> page(PageQO<UserQueryDTO> page) {
        PageVO<User> pageVO = identityHolder.page(page);
        return PageVO.of(pageVO.getPageNum(), pageVO.getPageSize(), pageVO.getTotal(), pageVO.getData().stream().map(identityMapping::entity2Vo).collect(Collectors.toList()));
    }
}